package com.xiguanlezz.cn.service;

import com.xiguanlezz.cn.UserServiceGrpc;
import com.xiguanlezz.cn.UserServiceProto;
import io.grpc.stub.StreamObserver;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author：习惯了种种
 * @CreateDate：2024/10/28 22:12
 * @Description：
 */
@Slf4j
public class UserServiceImpl extends UserServiceGrpc.UserServiceImplBase {
    @Override
    public void login(UserServiceProto.LoginRequest request, StreamObserver<UserServiceProto.LoginResponse> responseObserver) {
        // 获取参数
        String name = request.getName();
        String password = request.getPassword();

        // 业务处理
        log.info("name: {}, password: {}", name, password);

        // 封装响应结果
        UserServiceProto.LoginResponse response = UserServiceProto.LoginResponse.newBuilder()
                .setResult("登录成功！")
                .build();

        // 发送响应结果
        responseObserver.onNext(response);
        // 通知客户端服务处理完毕
        responseObserver.onCompleted();
    }

    @Override
    public void login2(UserServiceProto.LoginRequest request, StreamObserver<UserServiceProto.LoginResponse> responseObserver) {
        // 获取参数
        String name = request.getName();
        String password = request.getPassword();

        // 业务处理
        log.info("name: {}, password: {}", name, password);

        // 封装并逐条发送响应结果
        for (int i = 0; i < 5; i++) {
            log.info("发给客户端的响应-{}", i + 1);
            UserServiceProto.LoginResponse response = UserServiceProto.LoginResponse.newBuilder()
                    .setResult("登录成功")
                    .build();
            responseObserver.onNext(response);
        }

        // 通知客户端服务处理完毕
        responseObserver.onCompleted();
    }

    @Override
    public StreamObserver<UserServiceProto.LoginRequest> login3(StreamObserver<UserServiceProto.LoginResponse> responseObserver) {
        return new StreamObserver<UserServiceProto.LoginRequest>() {
            private List<UserServiceProto.LoginRequest> list = new ArrayList<>();

            @Override
            public void onNext(UserServiceProto.LoginRequest loginRequest) {
                list.add(loginRequest);
                log.info("接收到了客户端的一个请求。。。");
            }

            @Override
            public void onError(Throwable throwable) {

            }

            @Override
            public void onCompleted() {
                UserServiceProto.LoginResponse response = UserServiceProto.LoginResponse.newBuilder()
                        .setResult("登录成功！")
                        .build();
                log.info("接收到了客户端的所有请求: {}", list);
                // 等到接收到客户端的所有请求后，将结果发送给客户端
                responseObserver.onNext(response);
                // 通知客户端服务处理完毕
                responseObserver.onCompleted();
            }
        };
    }

    @Override
    public StreamObserver<UserServiceProto.LoginRequest> login4(StreamObserver<UserServiceProto.LoginResponse> responseObserver) {
        return new StreamObserver<UserServiceProto.LoginRequest>() {
            @Override
            public void onNext(UserServiceProto.LoginRequest loginRequest) {
                UserServiceProto.LoginResponse response = UserServiceProto.LoginResponse.newBuilder()
                        .setResult("登录成功！")
                        .build();
                // 接到客户端的一个请求后，将结果发送给客户端
                responseObserver.onNext(response);
                log.info("接收到了客户端的一个请求。。。");
            }

            @Override
            public void onError(Throwable throwable) {

            }

            @Override
            public void onCompleted() {
                log.info("接收到了客户端的所有请求。。。");
                responseObserver.onCompleted();
            }
        };
    }
}
